package com.sojpt.boot.controller.business;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyTableStorageAddValidator;
import com.sojpt.boot.controller.business.validator.LkyTableStorageUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyTableStorage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 本类用于
 * Created by JYN_LL on 2020/2/15 13:18
 */
@RestController
@RequestMapping("/admin/fileCheck/")
public class LkyFileCheckAdminController extends IViewController {
    @GetMapping("list")
    public void list() {
        Map<String,String> map =new HashMap<>();
        map.put("cid",getPara("cid"));
        map.put("mid",getPara("mid"));
        map.put("coid",getPara("coid"));
        map.put("uid",getPara("uid"));
        map.put("dtmId",getPara("dtmId"));
        System.out.println("map***************"+map.entrySet());
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");

        SqlPara sqlPara = Db.getSqlPara("lky_table_storage.allNotStudentFile", map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    /**
     * 老师批阅通过
     */
    @PostMapping("check")
    public void checkOk(){
        //日志
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/fileCheck/check");
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        //修改状状态为2 通过
        int count = Db.update("UPDATE lky_fill_in_form_record SET staute=2 WHERE table_storage_id = ?",lkyTableStorage.getId());
        if(count>0){
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("修改成功");
        }else{
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("修改失败");
        }

        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    /**
     * 老师批阅不通过
     */
    @PostMapping("checkNo")
    public void checkNo(){
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/fileCheck/check");
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        //修改状状态为3 不通过
        int count = Db.update("UPDATE lky_fill_in_form_record SET staute=3 WHERE table_storage_id = ?",lkyTableStorage.getId());
        if(count>0){
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("修改成功");
        }else{
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("修改失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }







    @PostMapping("save")
    @Before(LkyTableStorageAddValidator.class)
    public void save() {
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");

        if(lkyTableStorage.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("update")
    @Before(LkyTableStorageUpdateValidator.class)
    public void update() {
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        if(lkyTableStorage.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("delete")
    public void delete() {
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        if(LkyTableStorage.dao.deleteById(lkyTableStorage.getId())) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_table_storage.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

}
